/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.core.client.impl;

import com.google.gwt.dom.client.Element;

import me.l1k3.core.client.Core;
import me.l1k3.core.client.inter.ComputedStyleProperty;

public final class ComputedStylePropertyImplIE implements ComputedStyleProperty {
    
    @Override
    public final String getComputedStyleProperty(Element element, String property) {
        property = Core.toCamelCase(property);
        
        if(property.equals("float")) {
            property = "styleFloat";
        }
        
        return property.equals("opacity")? getComputedStyleOpacityImpl(element) : getComputedStylePropertyImpl(element, property);
    }
    
    @Override
    public final double getComputedStylePropertyAsPixel(Element element, String property) {
        property = Core.toCamelCase(property);
        return getValueAsPixel(element, getComputedStyleProperty(element, property), property);
    }
    
    private native double getValueAsPixel(Element e, String value, String property) /*-{
        if(/^\d+$/.test(value)) {
            return parseFloat(value);
        }
        
        var u = value.match(/^\d*\.?\d*\s*([\w%]+)$/)[1];
        
        if(u=="px") {
            return parseFloat(value);
        }
        
        if(u=="pt") {
            if(e.currentStyle) {
                var style = e.style, left = style.left, rsLeft = e.runtimeStyle.left;
                e.runtimeStyle.left = e.currentStyle.left;
                style.left = value || 0;
                var px = style.pixelLeft;
                style.left = left;
                e.runtimeStyle.left = rsLeft;
                return px;
            } 
            else {
                return Math.round(1.3333*parseFloat(value));       
            }
        }

        var height = /.*(Left|Right)$/.test(property) ? false : true,
        div = e.cloneNode(false);
        div.style.position = "absolute";
        div.style.visibility = "hidden";
        div.style.margin = 0;
        div.style.padding = 0;
        div.style.borderWidth = 0;
        div.style.overflow = "hidden";

        if(u=="%" || u=="em" || e.tagName=="IMG"){
            if(e.tagName!="BODY"){
                e = e.parentNode || e;
            }
            div.style.height = value;
        }
        else
        if(property=="lineHeight" || property=="fontSize") {
            try {
                div.innerHTML="x";
            }
            catch(err) { }
        }
        else
        if(height) {
            div.style.height = 0;
            div.style.lineHeight = 0;
            div.style.fontSize = 0;
        }

        div.style[property] = value;
        
        var parent = e.parentNode? e.parentNode : e;
        parent.appendChild(div);            
        var px = height ? div.offsetHeight : div.offsetWidth;     
        parent.removeChild(div);
        
        return parseFloat(px);
    }-*/;
    
    private native String getComputedStyleOpacityImpl(Element e) /*-{
        var value = 100;
        try {
            value = e.filters['DXImageTransform.Microsoft.Alpha'].opacity;
        } 
        catch(err1) {
            try {
                value = e.filters('alpha').opacity;
            } 
            catch(err2) { }
        }
        return String(value/100);
    }-*/;
    
    private native String getComputedStylePropertyImpl(Element e, String p) /*-{
        return (e.currentStyle)? String(e.currentStyle[p] || e.style[p]) : String(e.style[p]);
    }-*/;
}

